package com.syaccp.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.syaccp.model.SysUser;
import com.syaccp.service.SysMenuService;
import com.syaccp.service.UserService;

@Component("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {


    @Resource
    private UserService userService;
    
    @Resource
    private SysMenuService sysMenuService;
    
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = userService.loadUserByUsername(username);
        if (sysUser==null){
            throw  new UsernameNotFoundException("该账号不存在");
        }
        Integer roleId = sysUser.getRoleId();
        List<String> permissions =null;
        if (new Integer(1).equals(roleId)) {  //管理員獲取所有的權限
        	 permissions =sysMenuService.getAllPermissions();
        }else {
        	 permissions =sysMenuService.findPermissionByUserId(sysUser.getId());
        }
    
        sysUser.setPermissions(permissions);
        
        return sysUser;
    }
}
